<?php

/**
 * Pagenation
 *
 * @version $Id: Page.php 685 2009-08-03 06:28:25Z songxiang $
 * 
 */
class Page {
    /**
     * 每页条数
     */
    public $perPage = 10;
    /**
     * 当前页码
     */
    protected $currentPage = 1;
    /**
     * 总条数
     */
    public $total;
    
    public function __construct () {
        //初始化
        $this->currentPage = $_GET['page'] ? intval($_GET['page']) : 1;
    
    }
    /**
     * 
     * @return string like '0,10' for LIMIT use
     */
    public function getLimit () {
        return $this->perPage * ($this->currentPage - 1) . ',' . $this->perPage;
    }
    
    public function showLink () {
        $total_page = ceil($this->total / $this->perPage);
        $html = $this->currentPage . '/' . $total_page.'&nbsp';

        $min = $this->currentPage <= 5 ? 1 : ($this->currentPage - 5);
        $max = ($this->currentPage + 5) > $total_page ? $total_page : ($this->currentPage + 5);
        
        for ($i = $min; $i <= $max; $i ++) {
            if ($i != $this->currentPage) {
                $html .= "<span class='pcurpage'><a href=\"?page=$i\">$i</a></span>";
            } else {
                $html .= "<span class='pcurpage'>" . $i . '</span>'; //当前页不用带超链接
            }
        }
        
        return $html;
    }
    
    public function __toString () {
        return $this->showLink();
    }
}